package logic

import (
	"aliyun/aliyunrpc/aliyunrpc"
	"aliyun/aliyunrpc/internal/svc"
	"aliyun/models/mysql"
	"aliyun/models/redis"
	"context"
	"fmt"

	"github.com/zeromicro/go-zero/core/logx"
)

type RegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic {
	return &RegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 注册
func (l *RegisterLogic) Register(in *aliyunrpc.RegisterRequest) (*aliyunrpc.RegisterResponse, error) {
	// todo: add your logic here and delete this line
	get := redis.Rdb.Get(l.ctx, "sendSms"+in.Account)
	if get.Val() != in.SendSms {
		return nil, fmt.Errorf("验证码不正确")
	}
	var user mysql.AliyunUser
	mysql.DB.Where("account=?", in.Account).Find(&user)
	if user.ID != 0 {
		return nil, fmt.Errorf("用户已存在,请去登录")
	}
	users := mysql.AliyunUser{
		Account: in.Account,
	}
	mysql.DB.Create(&users)
	return &aliyunrpc.RegisterResponse{
		Id: int64(users.ID),
	}, nil
}
